//
//  MyBagViewController.swift
//  Eternity
//
//  Created by ZhengWei on 2016/11/27.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit

class MyBagViewController: BaseViewController {

    var isSelectedArr: [Bool]?
    var dataSource: Array<String>?
    var isEdit: Bool?
    
    
    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        dataSource = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
        isSelectedArr = [Bool]()
        dataSource?.forEach({ (_) in
            isSelectedArr?.append(false)
        })
        
        initNavItem(controller: self, imageName: "nav_edit_n", highlitedImageName: "nav_edit_h", selectedImageName: "nav_finish_n", isLeft: false)
        initBaseLayout()
        layoutPageSubviews()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    // MARK: - event response 
    override func rightItemAction(sender: UIButton) {
        if !sender.isSelected {
            sender.isSelected = true
            bottomView.isEdit = true
            
        } else {
            sender.isSelected = false
            bottomView.isEdit = false
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }

    
    // MARK: - private method
    func initBaseLayout() {
        self.view.addSubview(bagTableView)
        self.view.addSubview(bottomView)
    }
    
    func layoutPageSubviews() -> Void {
        bagTableView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view).inset(UIEdgeInsets(top: 0, left: 0, bottom: 50, right: 0))
        }
        bottomView.snp.makeConstraints { (make) in
            make.left.equalTo(self.view).offset(0)
            make.bottom.equalTo(self.view).offset(-50)
            make.width.equalTo(SCREEN_WIDTH)
            make.height.equalTo(50)
        }
    }
    
    func addSwipeButton(cell: MGSwipeTableCell) {
        let deleteBtn = MGSwipeButton(title: "删除", backgroundColor: UIColor(hexString: "#B50834"), callback: nil)
        deleteBtn?.frame = CGRect(x: 0, y: 0, width: 75, height: 142)
        deleteBtn?.setTitleColor(UIColor(hexString: "#FFFFFF"), for: .normal)
        deleteBtn?.titleLabel?.font = UIFont(name: "PingFangSC-Regular", size: 14)
        
        let collectBtn = MGSwipeButton(title: "移入" + "\n" + "收藏", backgroundColor: UIColor(hexString: "#B7A18A"), callback: nil)
        collectBtn?.frame = CGRect(x: 0, y: 0, width: 75, height: 142)
        collectBtn?.setTitleColor(UIColor(hexString: "#FFFFFF"), for: .normal)
        collectBtn?.titleLabel?.font = UIFont(name: "PingFangSC-Regular", size: 14)
        
        cell.rightButtons = [deleteBtn!, collectBtn!]
    }
    
    // MARK: - setters and getters
    var _bagTableView: UITableView!
    var bagTableView: UITableView {
        if _bagTableView == nil {
            _bagTableView = UITableView(frame: CGRect.zero, style: .grouped)
            _bagTableView.backgroundColor = UIColor(hexString: "#E5E5E5")
            _bagTableView.delegate = self
            _bagTableView.dataSource = self
            _bagTableView.register(MyBagCell.self, forCellReuseIdentifier: "MyBagCell")
        }
        return _bagTableView
    }
    
    var _bottomView: BottomView!
    var bottomView: BottomView {
        if _bottomView == nil {
            _bottomView = BottomView()
            _bottomView.backgroundColor = UIColor.white
            _bottomView.delegate = self
            _bottomView.isEdit = false
            _bottomView.configUI()
        }
        return _bottomView
    }
}

extension MyBagViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let string = "MyBagCell"
        let cell = tableView.dequeueReusableCell(withIdentifier: string, for: indexPath) as! MyBagCell
        
        cell.bagDelegate = self
        cell.indexPath = indexPath
        cell.configUI(isSelectedArr: isSelectedArr!, indexPath: indexPath)
        
        addSwipeButton(cell: cell)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = BranchHeader()
        header.section = section
        header.delegate = self
        
        return header
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 140
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }
}

extension MyBagViewController: MyBagCellDelegate {
    func selectProductAction(sender: UIButton) {
        if !sender.isSelected {
            sender.isSelected = true
            isSelectedArr?[sender.tag-100] = true
        } else {
            sender.isSelected = false
            isSelectedArr?[sender.tag-100] = false
        }
    }
}

extension MyBagViewController: BottomViewDelegate {
    func selectAllAction(sender: UIButton) {
        isSelectedArr?.removeAll()
        if !sender.isSelected {
            sender.isSelected = true
            dataSource?.forEach({ (_) in
                isSelectedArr?.append(true)
            })
        } else {
            sender.isSelected = false
            dataSource?.forEach({ (_) in
                isSelectedArr?.append(false)
            })
        }
        bagTableView.reloadData()
    }
}

extension MyBagViewController: BranchHeaderDelegate {
    func selectAllFromBranch(sender: UIButton) {
        
        if !sender.isSelected {
            sender.isSelected = true
        } else {
            sender.isSelected = false
        }
        
//        if sender.tag == 10 {
//            for i in 0 ..< 5 {
//                isSelectedArr?[i] = sender.isSelected
//            }
//        } else {
//            for i in 5 ..< 10 {
//                isSelectedArr?[i] = sender.isSelected
//            }
//        }
        
//        bagTableView.reloadData()
    }
}




